/*
@title: Undertale Sprig Edition
@description: "Undertale Sprig Edition" is a fan-made game inspired by Undertale's theme, where players are confined in a box and must dodge flying bones to increase their score. The challenge lies in maneuvering around the arena without getting hit by the obstacles, reminiscent of Undertale's unique battle mechanics. The objective is to survive and dodge as many bones as possible, steadily racking up points for each successful dodge.
@author: i-suck-at-most-stuff
@tags: []
@addedOn: 2024-00-00
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/
const playerRed = 'r';
const attackUp = 'a';
const attackSide = 's';
const fightOne = '1';
const fightTwo = '2';
const fightThree = '3';
const actOne = '4';
const actTwo = '5';
const actThree = '6';
const itemOne = '7';
const itemTwo = '8';
const itemThree = '9';
const mercyOne = '0';
const mercyTwo = '!';
const mercyThree = '@';
const sansOne = '#';
const sansTwo = '$';
const sansThree = '%';
const sansFour = '^';
const boundary = 'b';
const boundaryOtherSide = 'z';
const boundaryOtherSideTwo = 'l';
const boundaryOtherSideThree = 'g';
const cornerTwo = '<';
const cornerThree = '>';
const cornerFour = ',';
const cornerOne = ')';
const black = 'y';


setLegend(
  [playerRed, bitmap`
................
....00....00....
...0000..0000...
..003300003300..
.00333300333300.
0033333333333300
0333333333333330
0033333333333300
.00333333333300.
..003333333300..
...0033333300...
....00333300....
.....003300.....
......0000......
.......00.......
................`],
  [attackUp, bitmap`
................
......0..0......
.....020020.....
....02221220....
.....022210.....
......0120......
......0220......
......0210......
......0210......
......0220......
......0220......
.....021210.....
....02222220....
.....020020.....
......0..0......
................`],
  [attackSide, bitmap`
................
................
................
................
...0........0...
..020......020..
.02220000002220.
..021222221220..
..022221122210..
.02210000001220.
..020......020..
...0........0...
................
................
................
................`],
  [fightOne, bitmap`
9999999999999999
9000000000000000
9000000000000000
9000900099990999
9000990090000009
9000990090000009
9000990099900009
9000990090000009
9009990090000009
9099999090000009
9009900090000009
9009900090000009
9099000090000999
9000000000000000
9000000000000000
9999999999999999`],
  [fightTwo, bitmap`
9999999999999999
0000000000000000
0000000000000000
9909999009000909
0009000009000900
0009000009000900
0009000009000900
0009000009999900
0009000009000900
0009000009000900
0009009909000900
0009000909000900
9909999909000900
0000000000000000
0000000000000000
9999999999999999`],
  [fightThree, bitmap`
9999999900000000
0000000900000000
0000000900000000
9999900900000000
0090000900000000
0090000900000000
0090000900000000
0090000900000000
0090000900000000
0090000900000000
0090000900000000
0090000900000000
0090000900000000
0000000900000000
0000000900000000
9999999900000000`],
  [actOne, bitmap`
9999999999999999
9000000000000000
9000000000000000
9009999900009999
9000000900009000
9009990900009000
9000090900009000
9009090900009999
9009090900009000
9000090900009000
9009990900009000
9000000900009000
9009999900009000
9000000000000000
9000000000000000
9999999999999999`],
  [actTwo, bitmap`
9999999999999999
0000000000000000
0000000000000000
9900099999900099
0900090000000000
0900090000000000
0900090000000000
9900090000000000
0900090000000000
0900090000000000
0900090000000000
0900090000000000
0900099999900000
0000000000000000
0000000000000000
9999999999999999`],
  [actThree, bitmap`
9999999900000000
0000000900000000
0000000900000000
9999900900000000
0900000900000000
0900000900000000
0900000900000000
0900000900000000
0900000900000000
0900000900000000
0900000900000000
0900000900000000
0900000900000000
0000000900000000
0000000900000000
9999999900000000`],
  [itemOne, bitmap`
9999999999999999
9000000000000000
9000000000000000
9000000000099999
9000990000000090
9000990000000090
9000990000000090
9009999000000090
9099999900000090
9099999900000090
9099999900000090
9099999900000090
9009999000099999
9000000000000000
9000000000000000
9999999999999999`],
  [itemTwo, bitmap`
9999999999999999
0000000000000000
0000000000000000
9909999999099990
0000009000090000
0000009000090000
0000009000090000
0000009000099990
0000009000090000
0000009000090000
0000009000090000
0000009000090000
9900009000099990
0000000000000000
0000000000000000
9999999999999999`],
  [itemThree, bitmap`
9999999900000000
0000000900000000
0000000900000000
9999900900000000
9090900900000000
9090900900000000
9090900900000000
9090900900000000
9090900900000000
9090900900000000
9090900900000000
9090900900000000
9090900900000000
0000000900000000
0000000900000000
9999999900000000`],
  [mercyOne, bitmap`
9999999999999999
9000000000000000
9000000000000000
9090000090099999
9099000090090909
9009900990090909
9000909900090909
9000999000090909
9000099000090909
9000999000090909
9000909900090909
9009900900090909
9099000990090909
9000000000000000
9000000000000000
9999999999999999`],
  [mercyTwo, bitmap`
9999999999999999
0000000000000000
0000000000000000
0999009999009999
0900009009009000
0900009009009000
0900009999009000
0999009900009000
0900009990009000
0900009090009000
0900009099009000
0900009009009000
0999009009009999
0000000000000000
0000000000000000
9999999999999999`],
  [mercyThree, bitmap`
9999999900000000
0000000900000000
0000000900000000
0900090900000000
0090900900000000
0009000900000000
0009000900000000
0009000900000000
0009000900000000
0009000900000000
0009000900000000
0090000900000000
0900000900000000
0000000900000000
0000000900000000
9999999900000000`],
  [sansOne, bitmap`
0000000000000000
0000000222222222
0000022222222222
0000022222222222
0000222222222222
0000220002222200
0000220002222200
0000220002202200
0000022222000222
0000020222222222
0000220000000000
0000222020202020
0000002200000002
0000000022222220
0005000000000000
0005011022202220`],
  [sansTwo, bitmap`
0000000000000000
0000000000000000
2200000000000000
2200000000000000
2220000000000000
0220000000000000
0220000000000000
0220000000000000
2200000000000000
0200000000000000
0220000000000000
2220000000000000
2000000000000000
0000000000000000
0005000000000000
1105000000000000`],
  [sansThree, bitmap`
0055501100020001
0550050002202200
0555505502222205
0555550500222005
0005505502222205
0000505500000005
0000005500000005
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000022220000022
0002222200000002
0000000000000000
0000000000000000
0000000000000000`],
  [sansFour, bitmap`
1055500000000000
0500550000000000
5055550000000000
0555550000000000
5055500000000000
5050000000000000
5000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
2000000000000000
2220000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [boundary, bitmap`
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222`],
  [boundaryOtherSide, bitmap`
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000`],
  [boundaryOtherSideTwo, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [boundaryOtherSideThree, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
  [cornerOne, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000`],
  [cornerTwo, bitmap`
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
2222222000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [cornerThree, bitmap`
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [cornerFour, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222
0000000002222222`],
  [black, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
);

const level = map`
yyyyyyyyyyyyyy
yyyyyy#$yyyyyy
yyyyyy%^yyyyyy
,gggggggggggg)
byyyyyyyyyyyyz
byyyyyyyyyyyyz
byyyyyyyyyyyyz
>llllllllllll<
yyyyyyyyyyyyyy
y1234567890!@y`;
const start = map`
yyyyyyyyyyyyyy
yyyyyy#$yyyyyy
yyyyyy%^yyyyyy
yyyyyyyyyyyyyy
yyyyyyyyyyyyyy
yyyyyyyyyyyyyy
yyyyyyyyyyyyyy
yyyyyyyyyyyyyy
yyyyyyyyyyyyyy
yyyyyyyyyyyyyy`;


const megalovaniaTune = tune`
192.30769230769232: D4^192.30769230769232 + C4~192.30769230769232,
192.30769230769232: D4^192.30769230769232,
192.30769230769232: A5^192.30769230769232,
192.30769230769232: C4~192.30769230769232,
192.30769230769232: D5^192.30769230769232,
192.30769230769232,
192.30769230769232: C4~192.30769230769232 + B4^192.30769230769232,
192.30769230769232,
192.30769230769232: A4^192.30769230769232,
192.30769230769232: C4~192.30769230769232,
192.30769230769232: B4^192.30769230769232,
192.30769230769232,
192.30769230769232: D4^192.30769230769232 + C4~192.30769230769232,
192.30769230769232: G4^192.30769230769232,
192.30769230769232: B4^192.30769230769232,
192.30769230769232: C4~192.30769230769232 + D4^192.30769230769232,
192.30769230769232: D4^192.30769230769232,
192.30769230769232: A5^192.30769230769232,
192.30769230769232: B4^192.30769230769232 + C4~192.30769230769232,
192.30769230769232,
192.30769230769232: A4^192.30769230769232,
192.30769230769232: C4~192.30769230769232,
192.30769230769232: G4^192.30769230769232,
192.30769230769232,
192.30769230769232: D4^192.30769230769232 + C4~192.30769230769232,
192.30769230769232: G4^192.30769230769232,
192.30769230769232: B4^192.30769230769232,
192.30769230769232: C4~192.30769230769232,
192.30769230769232,
192.30769230769232: G5^192.30769230769232,
192.30769230769232: C4~192.30769230769232,
192.30769230769232`;

const pointSound = tune`
94.04388714733543: C5/94.04388714733543,
94.04388714733543: D5/94.04388714733543,
94.04388714733543: E5/94.04388714733543,
94.04388714733543: F5~94.04388714733543,
2633.228840125392`;
const hitSound = tune`
94.04388714733543: D5~94.04388714733543 + C5~94.04388714733543 + B4~94.04388714733543 + E5~94.04388714733543,
94.04388714733543: B4~94.04388714733543 + C5~94.04388714733543 + D5~94.04388714733543 + E5~94.04388714733543,
94.04388714733543: B4~94.04388714733543 + C5~94.04388714733543 + D5~94.04388714733543 + E5~94.04388714733543,
2727.2727272727275`;
setMap(start);

let gameStarted = false;

function playPointSound() {
  playTune(pointSound);
}


function playHitSound() {
  playTune(hitSound);
}
function showSplashScreen() {
  clearText();
  addText("it's a beautiful day", { x: 0, y: 6, color: color`2` });
  addText("outside. birds are", { x: 0, y: 7, color: color`2` });
  addText("singing, flowers are", { x: 0, y: 8, color: color`2` });
  addText("blooming. on days", { x: 0, y: 9, color: color`2` });
  addText("like these kids like", { x: 0, y: 10, color: color`2` });
  addText("you should be having", { x: 0, y: 11, color: color`2` });
  addText("fun with sprig", { x: 3, y: 12, color: color`3` });
  addText("Press any button", { x: 2, y: 14, color: color`2` });
}

function startGame() {
  clearText();
  gameStarted = true;
  setMap(level);
  playTune(megalovaniaTune, 1000000);
  addSprite(7, 6, playerRed);
  spawnAttackUp(); 
  setInterval(moveAttackUp, 100); 
  setInterval(handleCollision, 100); 
  setInterval(() => {
    
    clearText();
    addText(`Score: ${score}`, { x: 6 , y: 12, color: color`2` });
    addText(`High: ${highestScore}`, { x: 1 , y: 1, color: color`2` });
  }, 100); 
}

function onAnyInput() {
  if (!gameStarted) {
    clearText();
    setMap(level);
    setTimeout(countdown3, 500);
    setTimeout(countdown2, 1000);
    setTimeout(countdown1, 1500);
    setTimeout(startGame, 2000);
  }
}
showSplashScreen();

onInput("w", onAnyInput);
onInput("a", onAnyInput);
onInput("s", onAnyInput);
onInput("d", onAnyInput);
onInput("i", onAnyInput);
onInput("k", onAnyInput);
onInput("j", onAnyInput);
onInput("l", onAnyInput);
let highestScore = 1;
let score = 0;

function countdown3(){
addText(`3`, { x: 8 , y: 8, color: color`2` });
}
function countdown2(){
addText(`2`, { x: 9 , y: 8, color: color`2` });
}
function countdown1(){
addText(`1`, { x: 10 , y: 8, color: color`2` });
}

function updateHighestScore() {
  if (score > highestScore || score == highestScore) {
    highestScore = score;
  }
}
const boundaryLeft = 1;
const boundaryRight = width() - 2;
const boundaryTop = 4;
const boundaryBottom = 6;

onInput("w", () => {
  let player = getFirst(playerRed);
  if (player && player.y > boundaryTop) player.y -= 1;
});

onInput("s", () => {
  let player = getFirst(playerRed);
  if (player && player.y < boundaryBottom) player.y += 1;
});

onInput("a", () => {
  let player = getFirst(playerRed);
  if (player && player.x > boundaryLeft) player.x -= 1;
});

onInput("d", () => {
  let player = getFirst(playerRed);
  if (player && player.x < boundaryRight) player.x += 1;

});
onInput("i", () => {
  let player = getFirst(playerRed);
  if (player && player.y > boundaryTop) player.y -= 1;
});

onInput("k", () => {
  let player = getFirst(playerRed);
  if (player && player.y < boundaryBottom) player.y += 1;
});

onInput("j", () => {
  let player = getFirst(playerRed);
  if (player && player.x > boundaryLeft) player.x -= 1;
});

onInput("l", () => {
  let player = getFirst(playerRed);
  if (player && player.x < boundaryRight) player.x += 1;

});
let attackUpInterval = 3000; 
let attackUpSpawnPositions = [
  [1, 6],
  [1, 5],
  [1, 4]
];
let attackUpSpeed = 1; 

function spawnAttackUp() {
  let pos = attackUpSpawnPositions[Math.floor(Math.random() * attackUpSpawnPositions.length)];
  addSprite(pos[0], pos[1], attackUp);
}

function moveAttackUp() {
  let attack = getFirst(attackUp);
  if (attack) {
    if (attack.x < 12) {
      attack.x += attackUpSpeed;
      
      
    } else {
      attack.remove();
       
      playPointSound();
      score += 1;
      updateHighestScore();
      spawnAttackUp();
      
    }
  }
}
function handleCollision() {
  let attack = getFirst(attackUp);
  let player = getFirst(playerRed);
  if (attack && player && attack.x === player.x && attack.y === player.y) {
    attack.remove();
    playHitSound();
    score = 0;
    attackUpSpeed = 1;
    setTimeout(spawnAttackUp, 1000);
  }
}
setInterval(handleCollision, 1);
setInterval(moveAttackUp, 500);


